<template>
  <el-dialog
    class="dia-style"
    :title="title"
    :visible.sync="open"
    v-if="open"
    width="1000px"
    :close-on-click-modal="false"
    @close="cancel"
  >
    <el-form
      ref="form"
      :model="formData"
      label-width="140px"
      label-position="left"
      style="padding: 0 10px"
    >
      <!-- 基础信息部分 -->
      <div class="upload-title-box">
        <div class="upload-title">基础信息</div>
        <el-checkbox v-model="showRequired" @change="changeShow">仅展示必填信息</el-checkbox>
      </div>
      <HkForm
        ref="hkFormRef1"
        :formLabels="basicArr"
        :rules="formRules"
        :data="formData"
        :dict-list="dictList"
        :btn-hidden="true"
        labelWidth="140"
        labelPosition="left"
      >
        <template #filter-orgRegion>
          <el-cascader
            v-model="formData.orgRegion"
            :options="dictList.zoneList"
            :props="casProps"
            :filterable="true"
            placeholder="请选择所属区域"
            @change="handleChange"
          ></el-cascader>
        </template>

        <template #filter-parentId>
          <treeselect
            v-model="formData.parentId"
            :options="dictList.orgList"
            :normalizer="normalizer"
            placeholder="请选择上级部门"
          />
        </template>
      </HkForm>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import HkForm from '@/components/HKForm'
import Treeselect from '@riophae/vue-treeselect'
import { updateDept, addDept, getDept, queryByOrder } from '@/api/system/dept.js'
import AddTable from '@/components/Common/addTable.vue'
import { cloneDeep } from '@/utils/loadash-es'

export default {
  components: { HkForm, AddTable, Treeselect },
  data() {
    return {
      // 对话框是否显示
      open: false,
      // 仅展示必填信息
      showRequired: true,
      // 折叠菜单展示的数据
      activeNames: ['1', '2', '3', '4', '5'],
      casProps: { expandTrigger: 'hover', value: 'zoneCode', label: 'zoneName', children: 'children' },
      // 表单数据
      formData: {},
      // 基础信息
      basicArr: [
        {
          label: '部门编号',
          field: 'orgNo',
          type: 'input',
          maxlength: 20
        },
        {
          label: '部门名称',
          field: 'name',
          type: 'input',
          maxlength: 32
        },
        // {
        //   label: '别名',
        //   field: 'alias',
        //   type: 'input',
        //   maxlength: 32,
        //   hidden: true,
        // },
        {
          label: '上级部门',
          field: 'parentId',
          type: 'slot',
          hidden: false
        },
        {
          label: '机构类型',
          field: 'orgType',
          type: 'select',
          list: 'org_typeList'
        },
        // {
        //   label: '部门状态',
        //   field: 'status',
        //   type: 'select',
        //   list: 'statusList',
        //   hidden: true,
        // },
        {
          label: '单位类别',
          field: 'unitCategory',
          type: 'select',
          list: 'unitTypeList'
        },
        {
          label: '保密资质',
          field: 'secQualifications',
          type: 'select',
          list: 'orgQualificationList'
        },
        {
          label: '负 责 人',
          field: 'personLiable',
          type: 'input',
          hidden: true,
          maxlength: 32
        },

        {
          label: '联系电话',
          field: 'telephone',
          type: 'input',
          hidden: true,
          maxlength: 32
        },
        {
          label: '邮箱地址',
          field: 'email',
          type: 'input',
          hidden: true,
          maxlength: 32
        },
        {
          label: '机构排序',
          field: 'orders',
          type: 'number',
          hidden: true
        },
        {
          label: '所属区域',
          field: 'orgRegion',
          type: 'slot',
          hidden: true
        },

        {
          label: '详细地址信息',
          field: 'address',
          type: 'input',
          hidden: true,
          maxlength: 300
        },

        {
          label: '备注',
          field: 'remark',
          type: 'textarea',
          hidden: true,
          maxlength: 500,
          attrs: {
            rows: '8'
          },
          span: 24
        }
      ],
      // 表单校验
      formRules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        parentId: [{ required: true, message: '不能为空', trigger: 'blur' }],
        orgNo: [{ required: true, message: '不能为空', trigger: 'blur' }],
        orgType: [{ required: true, message: '不能为空', trigger: 'blur' }],
        secQualifications: [{ required: true, message: '不能为空', trigger: 'blur' }],
        unitCategory: [{ required: true, message: '不能为空', trigger: 'blur' }],
  /*       email: [
          {
            pattern: /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/,
            message: '请填写正确的邮箱',
            trigger: 'blur'
          }
        ], */
        // telephone: [{ pattern: /^1[34578]\d{9}$/, message: '请填写正确的电话', trigger: 'blur' }]
      },

      // 表格展示的数据
      tableData: [],
      // 表格列表的结构
      tableList: [
        { label: 'IP地址', param: 'equipmentUserId', type: 'input' },
        { label: 'MAC', param: 'equipmentOrgId', type: 'input' }
      ]
    }
  },
  props: {
    // 标题
    title: {
      type: String,
      default: ''
    },
    treeId: {
      type: String,
      default: ''
    },
    total: {
      type: Number,
      default: 0
    },
    dictList: {
      type: Object,
      default: () => {}
    }
  },
  computed: {},
  methods: {
    handleChange(data) {},
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      }
    },
    // 是否只展示必填项
    changeShow() {
      if (!this.showRequired && !this.formData.parentId && this.treeId) {
        this.formData.parentId = this.treeId
      }
      this.basicArr.forEach(item => {
        if (typeof item.hidden == 'boolean') {
          if (item.field == 'parentId') {
            if (this.formData.parentId === '0') item.hidden = true
            else item.hidden = false
          } else item.hidden = this.showRequired
        }
      })
    },
    // 清空表单
    resetFields() {
      this.$refs['form'].resetFields()
      this.$refs['hkFormRef1']?.resetForm('formLabels')
    },
    show(id) {
      if (!id) {
        this.formData = {}
      }
      this.$set(this.formData, 'orders', this.total+1)
      queryByOrder(id).then(res => {
        let size = res
       
        this.$set(this.formData, 'orgNo', size)
      })
      this.showRequired = true
      this.changeShow()

      return new Promise((resolve, reject) => {
        if (id) {
          getDept(id).then(res => {
            this.formData = res

            this.basicArr.forEach(item => {
              if (item.field == 'parentId') {
                if (this.formData.parentId === '0') {
                  item.hidden = true
                } else item.hidden = false
              }
            })
          })
        }
        resolve(this.formData)
      }).then(res => {
        this.open = true
      })
    },
    // 提交事件
    async submitForm() {
      //  let res = await this.$refs.add_table.submitTable();

      let res = await this.$refs.hkFormRef1.handleFormSubmit('formLabels')
      if (this.title == '添加') {
        addDept(this.formData).then(res => {
          this.$message.success('新增成功')
          this.open = false
          this.$emit('handleAddOff')
        })
      } else {
        updateDept(this.formData).then(res => {
          this.$message.success('修改成功')
          this.open = false
          this.$emit('handleAddOff')
        })
      }
    },
    // 关闭按钮
    cancel() {
      this.formData = {}
      this.open = false
    }
  },

  mounted() {},
  watch: {}
}
</script>

<style lang="scss" scoped>
.upload-title-box {
  height: 60px;
  width: 100%;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba($color: #000000, $alpha: 8%);

  .upload-title {
    font-size: 16px;
    font-weight: 700;
    color: #000000;
  }
}

::v-deep {
  .el-collapse-item {
    border-bottom: 1px solid #e6ebf5;

    .el-collapse-item__wrap {
      border-bottom: none;
    }

    .el-collapse-item__content {
      padding-bottom: 0px;
    }
  }

  .iRow {
    margin: 0 !important;
  }

  .el-dialog__body {
    display: flex;
    flex-direction: column;
  }

  .el-collapse {
    border-top: none;

    .el-collapse-item__header {
      height: 60px;
      font-size: 16px;
      font-weight: 700;
      color: #000000;
      margin-left: 30px;
      border-bottom: none;
    }

    .el-collapse-item__arrow {
      margin: 0 auto 0 10px;
      margin-left: -90px;
    }
  }
}

::v-deep .el-cascader {
  width: 100%;
}
::v-deep .el-dialog__body {
  max-height: 500px;
}
</style>
